package com.honeymoondesert.analyzer;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

import com.els.Elastic;
import com.els.util.QueryHelper;
import com.google.gson.JsonObject;
import com.honeymoondesert.util.PatternHelper;

public class AddressAnalyzer {
	public static JsonObject analyze(String address){
		String tempAddr=address;
		JsonObject result=new JsonObject();
		String regEx=".+路.*\\d+号";   //正则表达式
		String road=PatternHelper.getStrAsRegx(address, regEx);
		if(road!=null){
			tempAddr=address.replace(road, "");		//移除路名的地址名
			String number=PatternHelper.getStrAsRegx(road, "\\d+号").replace("号", "");
			String roadRGX=".+路";
			road=PatternHelper.getStrAsRegx(road, roadRGX);
			result.addProperty("road", road);		//路名
			result.addProperty("number", number);	//门牌号
		}
		regEx="[a-zA-Z]*\\d[楼层]";
		String floor=PatternHelper.getStrAsRegx(address, regEx);
		if(floor!=null){
			String floorRGX="[a-zA-Z]*\\d*";
			floor=PatternHelper.getStrAsRegx(floor,floorRGX);
			result.addProperty("floor", floor);
		}
		
		
		return result;
	}
	/**
	 * @author Chasel
	 * @param basestore 满记店铺
	 * @return 与参数满记店铺在同一栋楼层中的其他商家
	 */
	public static List<JsonObject>  getSameBuilding(JsonObject basestore){
		List<JsonObject> surroundList=new ArrayList<JsonObject>();
		Elastic els=new Elastic();
		Client client = els.getClient();
		JsonObject aa=AddressAnalyzer.analyze(basestore.get("address").getAsString());
//		String qString=aa.get("road").getAsString()+aa.get("number")+"号";
//		String[] columns={"address"};
//		float[] boosts={1f};
		BoolQueryBuilder queryBuilder=QueryBuilders.boolQuery();
		queryBuilder.must(QueryBuilders.termQuery("parent", basestore.get("id").getAsString()));
//		queryBuilder.must(QueryBuilders.termQuery("date", basestore.get("date").getAsString()));
		queryBuilder.must(QueryBuilders.termQuery("road", basestore.get("road").getAsString()));
		queryBuilder.must(QueryBuilders.termQuery("number", basestore.get("number").getAsString()));

		SearchRequestBuilder srb = client.prepareSearch("hmd_storeinfo");
		srb.setTypes("store");
		srb.setQuery(queryBuilder);
		srb.setScroll(new TimeValue(60000));
		SearchResponse sr = srb.execute().actionGet();
		System.out.println(sr.getHits().getTotalHits());
		do{
			for (SearchHit hit : sr.getHits().getHits()) {
				JsonObject surround=hitToJSON(hit);
				surround.addProperty("relation", "在同商城");
				surroundList.add(surround);
			}
			sr=client.prepareSearchScroll(sr.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
		}while(sr.getHits().getHits().length != 0);
		els.closeClient();
		return surroundList;
		
	}
	/**
	 * @author Chasel
	 * @param basestore 满记店铺
	 * @return 与参数满记店铺邻近大厦中的其他商家
	 */
	public static List<JsonObject>  getSurroundBuilding(JsonObject basestore){
		List<JsonObject> surroundList=new ArrayList<JsonObject>();
		Elastic els=new Elastic();
		Client client = els.getClient();
		JsonObject aa=AddressAnalyzer.analyze(basestore.get("address").getAsString());
//		String qString=aa.get("road").getAsString()+aa.get("number")+"号";
//		String[] columns={"address"};
//		float[] boosts={1f};
		BoolQueryBuilder queryBuilder=QueryBuilders.boolQuery();
		queryBuilder.must(QueryBuilders.termQuery("parent", basestore.get("id").getAsString()));
//		queryBuilder.must(QueryBuilders.termQuery("date", basestore.get("date").getAsString()));
		queryBuilder.must(QueryBuilders.termQuery("road", basestore.get("road").getAsString()));
		Integer number=basestore.get("number").getAsInt();
		queryBuilder.must(QueryBuilders.rangeQuery("number").from(number-50).to(number+50));
		queryBuilder.mustNot(QueryBuilders.termQuery("number", number));

		SearchRequestBuilder srb = client.prepareSearch("hmd_storeinfo");
		srb.setTypes("store");
		srb.setQuery(queryBuilder);
		srb.setScroll(new TimeValue(60000));
		SearchResponse sr = srb.execute().actionGet();
		System.out.println(sr.getHits().getTotalHits());
		do{
			for (SearchHit hit : sr.getHits().getHits()) {
				JsonObject surround=hitToJSON(hit);
				surround.addProperty("relation", "在同商城");
				surroundList.add(surround);
			}
			sr=client.prepareSearchScroll(sr.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
		}while(sr.getHits().getHits().length != 0);
		els.closeClient();
		return surroundList;
		
	}
	/**
	 * @author Chasel
	 * @param basestore 满记店铺
	 * @return 除去语义分析层的，利用百度接口取出的附近楼层
	 */
	public static List<JsonObject>  getSurroundBuildingByBaidu(JsonObject basestore,Integer radius){
		List<JsonObject> surroundList=new ArrayList<JsonObject>();
		Elastic els=new Elastic();
		Client client = els.getClient();
		JsonObject aa=AddressAnalyzer.analyze(basestore.get("address").getAsString());
//		String qString=aa.get("road").getAsString()+aa.get("number")+"号";
//		String[] columns={"address"};
//		float[] boosts={1f};
		BoolQueryBuilder mainQuery=QueryBuilders.boolQuery();
		BoolQueryBuilder queryBuilder=QueryBuilders.boolQuery();
		queryBuilder.must(QueryBuilders.termQuery("parent", basestore.get("id").getAsString()));
//		queryBuilder.must(QueryBuilders.termQuery("date", basestore.get("date").getAsString()));
		queryBuilder.must(QueryBuilders.termQuery("road", basestore.get("road").getAsString()));
		Integer number=basestore.get("number").getAsInt();
		queryBuilder.must(QueryBuilders.rangeQuery("number").from(number-50).to(number+50));
		mainQuery.mustNot(queryBuilder);
		mainQuery.must(QueryBuilders.rangeQuery("distance").lte(radius).gte(0));
		mainQuery.must(QueryBuilders.termQuery("parent", basestore.get("id").getAsString()));
		
		
		SearchRequestBuilder srb = client.prepareSearch("hmd_storeinfo");
		srb.setTypes("store");
		srb.setQuery(mainQuery);
		srb.setScroll(new TimeValue(60000));
		SearchResponse sr = srb.execute().actionGet();
		System.out.println(sr.getHits().getTotalHits());
		do{
			for (SearchHit hit : sr.getHits().getHits()) {
				JsonObject surround=hitToJSON(hit);
				surround.addProperty("relation", "在同商城");
				surroundList.add(surround);
			}
			sr=client.prepareSearchScroll(sr.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
		}while(sr.getHits().getHits().length != 0);
		els.closeClient();
		return surroundList;
		
	}
	private static JsonObject hitToJSON(SearchHit hit){
		JsonObject surround=new JsonObject();
		surround.addProperty("id", hit.getSource().get("id").toString());
		surround.addProperty("storeName", hit.getSource().get("storeName").toString());
		surround.addProperty("district",  hit.getSource().get("district").toString());
		surround.addProperty("address",  hit.getSource().get("address").toString());
		surround.addProperty("tel",  hit.getSource().get("tel").toString());
		surround.addProperty("commentNum", hit.getSource().get("commentNum").toString());
		surround.addProperty("score", hit.getSource().get("score").toString());
		surround.addProperty("advPrice",  hit.getSource().get("advPrice").toString());
		surround.addProperty("enviroment", hit.getSource().get("enviroment").toString());
		surround.addProperty("service",  hit.getSource().get("service").toString());
		surround.addProperty("date",  hit.getSource().get("date").toString());
		surround.addProperty("storeUrl",  hit.getSource().get("storeUrl").toString());
		surround.addProperty("lng",  hit.getSource().get("lng").toString());
		surround.addProperty("lat",  hit.getSource().get("lat").toString());
		surround.addProperty("storeType", hit.getSource().get("storeType").toString());
		surround.addProperty("distance", hit.getSource().get("distance").toString());
		return surround;
	}
	public static void main(String[] args) {
		String manjiAddr="徐家汇路618号日月光广场B2楼F-11(近瑞金二路)";
		JsonObject roadinfo=AddressAnalyzer.analyze(manjiAddr);
		JsonObject basestore=new JsonObject();
		basestore.addProperty("address", manjiAddr);
		basestore.addProperty("id", 4234082);
		basestore.addProperty("date", "2015-10-29");
		basestore.addProperty("road", roadinfo.get("road").getAsString());
		basestore.addProperty("number", roadinfo.get("number").getAsString());
		basestore.addProperty("floor", roadinfo.get("floor").getAsString());
		List<JsonObject> sameList=AddressAnalyzer.getSameBuilding(basestore);

		for (JsonObject jsonObject : sameList) {
			System.out.println(jsonObject);
		}
		List<JsonObject> surroundList=AddressAnalyzer.getSurroundBuilding(basestore);
		for (JsonObject jsonObject : surroundList) {
			System.out.println(jsonObject);
		}
		List<JsonObject> surroundBaiduList=AddressAnalyzer.getSurroundBuildingByBaidu(basestore,1000);
		for (JsonObject jsonObject : surroundBaiduList) {
			System.out.println(jsonObject);
		}
		
		
		
	}
}
